package com.googlecode.stemwijzer.client.presenter;

import net.customware.gwt.presenter.client.EventBus;
import net.customware.gwt.presenter.client.place.Place;
import net.customware.gwt.presenter.client.widget.WidgetDisplay;

import com.allen_sauer.gwt.log.client.Log;
import com.google.inject.Inject;
import com.googlecode.stemwijzer.client.event.ExceptionEvent;
import com.googlecode.stemwijzer.client.event.NotificationEvent;
import com.googlecode.stemwijzer.client.event.RegistrationEvent;
import com.googlecode.stemwijzer.client.event.handler.ExceptionHandler;
import com.googlecode.stemwijzer.client.event.handler.NotificationHandler;
import com.googlecode.stemwijzer.client.event.handler.RegistrationHandler;

public class NotificationPresenter extends AbstractPresenter<NotificationPresenter.Display> implements
		NotificationHandler, ExceptionHandler, RegistrationHandler {

	public interface Display extends WidgetDisplay {
		void showMessage(String message);

		void showError(String errorMsg);
	}

	public static final Place PLACE = new Place("NotificationPresenter");

	@Inject
	public NotificationPresenter(final Display display, final EventBus eventBus) {
		super(display, eventBus);

		bind();
	}

	@Override
	protected void onBind() {
		super.onBind();

		registerHandler(eventBus.addHandler(RegistrationEvent.TYPE, this));
		registerHandler(eventBus.addHandler(ExceptionEvent.TYPE, this));
		registerHandler(eventBus.addHandler(NotificationEvent.TYPE, this));
	}

	@Override
	public Place getPlace() {
		return PLACE;
	}

	@Override
	public void onRegistration(RegistrationEvent event) {
		Log.debug("Handling registration event");

		this.display.showMessage(event.getUserName() + " is geregistreerd");
	}

	@Override
	public void onException(ExceptionEvent event) {
		Log.debug("Handling exception event");

		this.display.showError(event.getMessage());
	}

	@Override
	public void onNotification(NotificationEvent event) {
		Log.debug("Handling notification event");

		this.display.showMessage(event.getMessage());
	}
}